package br.com.lopes.gci.service.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Service;

import br.com.lopes.gci.exception.MailServiceException;
import br.com.lopes.gci.service.IMailService;

@Service
public class MailService implements IMailService {
	
	@Autowired
	private MailSender mailSender;
	
	@Autowired
	private SimpleMailMessage mailMessage;

	public void send(final Map<String, String> mapData) throws MailServiceException{
		//Create a thread safe "sandbox" of the message
		SimpleMailMessage message = new SimpleMailMessage(this.mailMessage);
		message.setTo(mapData.get("mail.to"));
		message.setSubject(mapData.get("mail.subject"));
		message.setText(mapData.get("mail.message"));
		try{
			mailSender.send(message);
		} catch(MailException ex) {
			throw new MailServiceException(ex);
		}
	}

	/**
	* @return the mailSender
	*/
	public MailSender getMailSender() {
		return mailSender;
	}
	
	/**
	* @param mailSender the mailSender to set
	*/
	public void setMailSender(MailSender mailSender) {
		this.mailSender = mailSender;
	}
	
	/**
	* @return the mailMessage
	*/
	public SimpleMailMessage getMailMessage() {
		return mailMessage;
	}
	
	/**
	* @param mailMessage the mailMessage to set
	*/
	public void setMailMessage(SimpleMailMessage mailMessage) {
		this.mailMessage = mailMessage;
	}

}

